package com.hl.toolkit.server.tycoin.account.slaveservice;

import com.tycoin.account.slaveentity.TycoinAccount;
import com.tycoin.account.slaveentity.TycoinAccountLog;
import com.tycoin.account.slaveentity.TycoinEthLog;
import com.tycoin.account.slaverepository.TycoinAccountLogRepository;
import com.tycoin.account.slaverepository.TycoinAccountRepository;
import com.tycoin.account.slaverepository.TycoinEthLogRepository;
import com.tycoin.user.slaveentity.TycoinUser;
import com.tycoin.user.slaverepository.TycoinUserRepository;
import com.tycoin.user.slaveservice.TycoinUserService;
import com.util.AmountUtil;
import com.util.DateUtils;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(value = "slaveTransactionManager")			 
public class TycoinUserTransferAccountService  {

	@Autowired
	TycoinAccountRepository tycoinAccountRepository;
	@Autowired
	TycoinAccountLogRepository tycoinAccountLogRepository;
	@Autowired
	TycoinEthLogRepository tycoinEthLogRepository;
	@Autowired
	TycoinUserRepository tycoinUserRepository;
	@Autowired
	TycoinUserService tycoinUserService;
	
 
	public Map<String,Object> transferAccount(Map<String,Object> map) {
		Map<String,Object> resultMap=new HashMap<String, Object>();
		boolean flag=false;

		
		String realname=map.get("realname").toString();
		String mobile=map.get("mobile").toString();
		String money=map.get("money").toString();
		String remark=map.get("remark").toString();
		//打款方的userId
		String userId=map.get("userId").toString();		
		
		if(StringUtil.isNull(userId)){
			resultMap.put("code", "203");
			resultMap.put("msg", "userId不能为空");
		}else if(StringUtil.isNull(realname)){
			resultMap.put("code", "204");
			resultMap.put("msg", "realname不能为空");
		}else if(StringUtil.isNull(money)|Integer.parseInt(money)<0){
			resultMap.put("code", "205");
			resultMap.put("msg", "money不能为空并且不能小余0");
		}else if(StringUtil.isNull(mobile)){
			resultMap.put("code", "206");
			resultMap.put("msg", "mobile不能为空");
		}else{
			//收款方的用户信息
			TycoinUser tycoinUser=tycoinUserRepository.findOneBymobile(mobile);
			
			if(tycoinUser==null){
				resultMap.put("code", "201");
				resultMap.put("msg", "用户不存在");			
			}else if(!tycoinUser.getRealname().equals(realname)){
				resultMap.put("code", "210");
				resultMap.put("msg", "真实姓名错误");	
			}
			else if(userId.equals(tycoinUser.getId())){
				resultMap.put("code", "207");
				resultMap.put("msg", "请勿自己给自己打款");
			}else{
//				//打款方用户信息
//				TycoinUser outTycoinUser = tycoinUserService.findById(Long.valueOf(userId));
//				if(outTycoinUser.getIsInside()==1){
//					flag=true;
//				}else if(tycoinUser.getPid()!=null && tycoinUser.getPid()!=0 ){
//					if((""+tycoinUser.getPid()).equals(userId)){
//						flag=true;
//					}
//				}	
				//打款方账户
				TycoinAccount outAccount=tycoinAccountRepository.findOneByuserId( userId);
				if(AmountUtil.sub(String.valueOf(outAccount.getUseMoney()), money).floatValue()<0){
					resultMap.put("code", "202");
					resultMap.put("msg", "余额不足");
				}else{
					//收款方账户
					System.out.println("收款的Id:"+tycoinUser.getId());
					String inUserId=String.valueOf(tycoinUser.getId());
					System.out.println("收款的Id:"+inUserId);
					TycoinAccount inAccount=tycoinAccountRepository.findOneByuserId( inUserId);
					if(inAccount==null){
						resultMap.put("code", "208");
						resultMap.put("msg", "收款方账户不存在!");
					}else{						
						if(flag){							
							long dateTime=System.currentTimeMillis();
							 String tranNo =  "ZZ"+dateTime  + (long) (Math.random() * 10000L);							
						//修改账户积分金额
							//---获取双方账户原有积分余额
						String outMoney=String.valueOf(outAccount.getUseMoney());
						String inMoney=String.valueOf(inAccount.getUseMoney());
						//1.修改打款方积分余额
						outAccount.setUseMoney(AmountUtil.sub(String.valueOf(outAccount.getUseMoney()), money));
						tycoinAccountRepository.save(outAccount);
						//2.添加打款方积分余额
						outAccount.setUseMoney(AmountUtil.sub(String.valueOf(inAccount.getUseMoney()), money));
 						tycoinAccountRepository.save(inAccount);
						//添加积分账户日志
						//1.添加打款方账积分账户日志记录
						TycoinAccountLog  outTycoinAccountLog=new TycoinAccountLog();
						outTycoinAccountLog.setUserId(String.valueOf(userId));
						outTycoinAccountLog.setType("1");
						outTycoinAccountLog.setTypeName("积分转账");
						outTycoinAccountLog.setFromId(String.valueOf(tycoinUser.getId()));
						outTycoinAccountLog.setNum(-Float.parseFloat(money));
						outTycoinAccountLog.setRealNum(-Float.parseFloat(money));
						outTycoinAccountLog.setStatus(1);
						outTycoinAccountLog.setRemark("["+userId+"]用户向["+tycoinUser.getId()+"]用户转账："+"["+money+"]");
						outTycoinAccountLog.setAddtime(System.currentTimeMillis());
						outTycoinAccountLog.setTranNo(tranNo);
						outTycoinAccountLog.setTransferMemo(remark);
						tycoinAccountLogRepository.save(outTycoinAccountLog);
						//2.添加收款方账积分账户日志记录
						TycoinAccountLog  inTycoinAccountLog=new TycoinAccountLog();
						inTycoinAccountLog.setUserId(String.valueOf(tycoinUser.getId()));
						inTycoinAccountLog.setType("1");
						inTycoinAccountLog.setTypeName("积分转账");
//						inTycoinAccountLog.setFromId(String.valueOf(outTycoinUser.getId()));
						inTycoinAccountLog.setNum(+Float.parseFloat(money));
						inTycoinAccountLog.setRealNum(+Float.parseFloat(money));
						inTycoinAccountLog.setStatus(1);
						inTycoinAccountLog.setRemark("["+tycoinUser.getId()+"]用户收到["+userId+"]用户转账："+"["+money+"]");
						inTycoinAccountLog.setAddtime(System.currentTimeMillis());
						inTycoinAccountLog.setTranNo(tranNo);
						inTycoinAccountLog.setTransferMemo(remark);						
						tycoinAccountLogRepository.save(inTycoinAccountLog);			
						//添加积分记录表
						//1.添加打款方积分记录表
						TycoinEthLog outTycoinEthLog=new TycoinEthLog();
						outTycoinEthLog.setUserId(String.valueOf(userId));
						outTycoinEthLog.setOldNum(Float.parseFloat(outMoney));
						outTycoinEthLog.setNum(-Float.parseFloat(money));
						outTycoinEthLog.setNewNum((AmountUtil.sub(outMoney, money).floatValue()));
						outTycoinEthLog.setFromType("积分转账");
						outTycoinEthLog.setActDate(DateUtils.getSystemTimeYMDByDate());
						outTycoinEthLog.setRemark("");
						outTycoinEthLog.setAddtime(System.currentTimeMillis());
						outTycoinEthLog.setAddip("127.0.0.1");
						
						tycoinEthLogRepository.save(outTycoinEthLog);
						//2.添加收款方积分记录表
						TycoinEthLog inTycoinEthLog=new TycoinEthLog();
						inTycoinEthLog.setUserId(String.valueOf(tycoinUser.getId()));
						inTycoinEthLog.setOldNum(Float.parseFloat(inMoney));
						inTycoinEthLog.setNum(Float.parseFloat(money));
						inTycoinEthLog.setNewNum((AmountUtil.add(inMoney, money).floatValue()));
						inTycoinEthLog.setFromType("积分转账");
						inTycoinEthLog.setActDate(DateUtils.getSystemTimeYMDByDate());
						inTycoinEthLog.setRemark(remark);
						inTycoinEthLog.setAddtime(System.currentTimeMillis());
						inTycoinEthLog.setAddip("127.0.0.1");
						tycoinEthLogRepository.save(inTycoinEthLog);
						resultMap.put("code", "200");
						resultMap.put("msg", "转账完成");
						}else{
							resultMap.put("code", "209");
							resultMap.put("msg", "该收款方不是自己的客户！");
						}
					}
					
					
				}
				
			}
		}
		System.out.println("resultMap="+resultMap);
		return resultMap;
	}

}
